"""
将MySQL中的数据写入到文件中
"""
from pymysql import Connection
from data_define import Record
import os

# 创建MySQL链接
conn = Connection(
    host="localhost",
    port=3306,
    user="root",
    password="123456",
    autocommit=True  # 自动提交
)
conn.select_db("test")
cursor = conn.cursor()

# 查询数据
cursor.execute("select * from orders")
result = cursor.fetchall()
conn.close()

# 创建文件
f = open(f"{os.getcwd()}/my_data.txt", "w", encoding="UTF-8")

# print(result)
for temp in result:
    f.write(str(Record(str(temp[0]), temp[1], temp[2], temp[3])) + "\n")

f.flush()
f.close()
